package com.tssm.action.usercenter;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionSupport;
import com.tssm.cache.memcached.TsSessionUtil;
import com.tssm.core.util.Pages;
import com.tssm.pojo.SarcQuotation;
import com.tssm.pojo.SarcQuotationCargo;
import com.tssm.pojo.SarcRefer;
import com.tssm.pojo.SarcSupplier;
import com.tssm.service.SarcQuotationService;
import com.tssm.service.SarcReferService;

@Controller("QuotationAction")
@Scope("prototype")
public class QuotationAction extends ActionSupport {

	private static final long serialVersionUID = 8439962187744710863L;
	@Resource
	private SarcQuotationService quotationService;
	@Resource
	private SarcReferService sarcReferService;
	//业务组件
	private SarcSupplier ssup;
	private SarcQuotation model = new SarcQuotation();
	private List<SarcQuotation> modelList = new ArrayList<SarcQuotation>();
	private List<SarcQuotationCargo> modelbody;
	private Pages pages = new Pages();
	//业务参数
	private Long id;
	private String action;
	private String deleteId;
	
	public String insertQuotation() {
		HttpServletRequest request = ServletActionContext.getRequest();
		ssup = TsSessionUtil.getTsUser(request);
		if(action != null && "insert".equals(action)) {
			model.setDef9(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
			return SUCCESS;
		}
		if(action != null && "view".equals(action)) {
			model = quotationService.get(SarcQuotation.class, id);
			modelbody = quotationService.queryQuotationCargo(id);
			return SUCCESS;
		}
		if(action != null && "save".equals(action)) {
			model.setStatus("0");
			model.setDr(0L);
			quotationService.saveQuotation(model, modelbody, deleteId);
		}
		if(action != null && "submit".equals(action)) {
			model.setStatus("1");
			quotationService.saveQuotation(model, modelbody, deleteId);
		}
		return "toQuery";
	}
	
	public String queryQuotation() {
		HttpServletRequest request = ServletActionContext.getRequest();
		ssup = TsSessionUtil.getTsUser(request);
		pages.setPages(quotationService.queryQuotationCount(ssup), 20);
		modelList = quotationService.queryQuotation(ssup, pages);
		return SUCCESS;
	}
	
	public String operateQuotation() {
		if(action != null && "submit".equals(action)) {
			model = quotationService.get(SarcQuotation.class, id);
			model.setStatus("1");
			quotationService.saveOrUpdate(model);
		}
		return "toQuery";
	}
	
	public String deleteQuotation() {
		quotationService.deleteQuotation(id);
		return "toQuery";
	}

	public SarcSupplier getSsup() {
		return ssup;
	}

	public void setSsup(SarcSupplier ssup) {
		this.ssup = ssup;
	}

	public SarcQuotation getModel() {
		return model;
	}

	public void setModel(SarcQuotation model) {
		this.model = model;
	}

	public List<SarcQuotation> getModelList() {
		return modelList;
	}

	public void setModelList(List<SarcQuotation> modelList) {
		this.modelList = modelList;
	}

	public Pages getPages() {
		return pages;
	}

	public void setPages(Pages pages) {
		this.pages = pages;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getAction() {
		return action;
	}

	public void setAction(String action) {
		this.action = action;
	}

	public List<SarcQuotationCargo> getModelbody() {
		return modelbody;
	}

	public void setModelbody(List<SarcQuotationCargo> modelbody) {
		this.modelbody = modelbody;
	}

	public String getDeleteId() {
		return deleteId;
	}

	public void setDeleteId(String deleteId) {
		this.deleteId = deleteId;
	}
		
}
